package com.hippo.core.ui.compose.login

import LoadingDialog
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.components.InputEdit
import com.hippo.core.ui.components.PasswordEdit
import com.hippo.core.ui.components.SecondaryButton
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.compose.register.UserAgreementDialog
import com.hippo.core.ui.theme.ColorBackgroundPrimary
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.PaddingMedium
import com.hippo.core.utils.AppUtil
import com.hippo.core.vm.LoginVM



@Composable
fun LoginScreen(
    toMain: () -> Unit,
    toForgetPassword: () -> Unit,
    toRegistry: () -> Unit,
    vm:LoginVM = viewModel(),
) {

    val showProtocolDialog = remember { mutableStateOf(false) }
    val showProtocolDialog2 = remember { mutableStateOf(false) }

    val uiState = vm.uiState

    val context = LocalContext.current

    if(uiState.isLoading){
        LoadingDialog() {
            vm.dismissLoading()
        }
    }

    LaunchedEffect(uiState) {
        if(uiState.isLogin == true){
            toMain()
        }
    }


    if(showProtocolDialog.value){
        UserAgreementDialog(
            onDismissRequest = {
                showProtocolDialog.value = false
            },
            onAgree = {
                showProtocolDialog.value = false
                vm.login{
                    toMain()
                }
            },
        )
    }

    if(showProtocolDialog2.value){
        UserAgreementDialog(
            onDismissRequest = {
                showProtocolDialog2.value = false
            },
            onAgree = {
                showProtocolDialog2.value = false
                toRegistry()
            },
        )
    }


    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(color = ColorBackgroundPrimary)
            .padding(PaddingMedium)
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
        ) {

            VerticalSpacer(20.dp, modifier = Modifier.weight(1F))

            Image(
                painter = painterResource(id = R.mipmap.login_logo),
                contentDescription = "logo",
                modifier = Modifier
                    .size(100.dp)
                    .align(Alignment.CenterHorizontally)
            )

            VerticalSpacer(20.dp, modifier = Modifier.weight(1F))

            InputEdit(
                modifier = Modifier.fillMaxWidth(),
                text = uiState.userEmail.orEmpty(),
                hint = stringResource(R.string.login_2),
                onValueChange = {
                    vm.updateEmail(email = it)
                },
                leftPainter = painterResource(R.mipmap.mail),
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Email
                )
            )

            VerticalSpacer(15.dp)

            PasswordEdit(
                modifier = Modifier.fillMaxWidth(),
                text = uiState.password.orEmpty(),
                hint = stringResource(R.string.login_3),
                onValueChange = {
                    vm.updatePassword(password = it)
                },
                leftPainter = painterResource(R.mipmap.pwd),
            )

            VerticalSpacer(15.dp)

            ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.login_4),
                onClick = {

                    if(uiState.userEmail.isNullOrBlank()){
                        Toast.makeText(context, context.getString(R.string.login_2), Toast.LENGTH_SHORT).show()
                        return@ThemeButton
                    }

                    if(uiState.password.isNullOrBlank()){
                        Toast.makeText(context, context.getString(R.string.login_3_1), Toast.LENGTH_SHORT).show()
                        return@ThemeButton
                    }

                    if(!AppUtil.isValidEmail(uiState.userEmail)){
                        Toast.makeText(context, context.getString(R.string.login_2), Toast.LENGTH_SHORT).show()
                        return@ThemeButton
                    }

                    showProtocolDialog.value = true
                }
            )

            VerticalSpacer(20.dp)

            Text(
                text = stringResource(R.string.login_5),
                color = MaterialTheme.colorScheme.primary,
                textDecoration = TextDecoration.Underline,
                modifier = Modifier
                    .align(Alignment.CenterHorizontally)
                    .click {
                        toForgetPassword()
                    }
            )

            VerticalSpacer(20.dp, modifier = Modifier.weight(2F))

            SecondaryButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.login_6),
                onClick = {
                    showProtocolDialog2.value = true
                }
            )

            VerticalSpacer(20.dp, modifier = Modifier.weight(1F))
        }

    }

}


@Preview
@Composable
private fun LoginScreenPreview() {
    HippoTheme {
//        LoginScreen(
//            vm = LoginVM(),
//            toMain = {},
//            toForgetPassword = {},
//            toRegistry = {}
//        )
    }
}